/*
 * @Description: vuex
 * @Version: 2.0
 * @Autor: Seven
 * @Date: 2020-12-09 13:13:35
 * @LastEditors: Seven
 * @LastEditTime: 2020-12-22 13:20:51
 */
export const state = () => ({
    // 是否登录
    isLogin: false,
    // token
    token:'',
    // 用户信息
    user:'',
    // 底部信息
    footer:''
  })
  
export const mutations = {
    set_isLogin(state,val) {
        state.isLogin = val
    },
    set_user(state,val){
        state.user = val
    },
    set_token(state,val){
        state.token = val
    }
}

export const actions = {
    async nuxtServerInit ({ commit }, { req,app }) {
        //获取token
        let token = app.$cookies.get('token')
        if(token){
            commit('set_token',token)
            // 请求用户信息
            let data = await app.$axios.$post('/pc/user/info').then(res=>{
                if(res.code === 0){
                    return res.data
                }
            })
            commit('set_user',data)
            commit('set_isLogin',true)
        }
    },
    // 退出登录
    outLogin({commit}){
        // 删除token
        document.cookie = 'token=; expires=Thu, 01-Jan-1970 00:00:01 GMT';
        commit('set_user','')
        commit('set_isLogin',false)
    }
}